package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.aitoedu.constant.MessageConstant;
import com.example.aitoedu.dto.CreateCourseDTO;
import com.example.aitoedu.dto.MyStudentDTO;
import com.example.aitoedu.entity.Course;
import com.example.aitoedu.entity.Student;
import com.example.aitoedu.exception.UploadErrorException;
import com.example.aitoedu.mapper.CourseMapper;
import com.example.aitoedu.mapper.ScMapper;
import com.example.aitoedu.mapper.StudentMapper;
import com.example.aitoedu.service.CourseService;
import com.example.aitoedu.utils.AliOSSUtils;
import com.example.aitoedu.vo.CourseCreateVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
* @author 曹正豪
* @description 针对表【course(课程信息)】的数据库操作Service实现
* @createDate 2024-10-14 19:14:27
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ScMapper scMapper;

    @Override
    public List<Course> myCourse() {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Course> courses = courseMapper.selectList(new QueryWrapper<Course>().eq("tid",tid));
        return courses;
    }

    @Override
    public CourseCreateVO createCourse(MultipartFile file, String cname){
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        int code;
        while(true){
            Random random = new Random();
            code = random.nextInt(900000) + 100000;
            Course course = courseMapper.selectOne(queryWrapper.eq("code", code));
            if(course == null) break;
        }
        String upload="https://czh-springboot.oss-cn-beijing.aliyuncs.com/firstpicture.jpg";
        if (file != null) {
            try {
                upload = aliOSSUtils.upload(file);
            } catch (IOException e) {
                throw new UploadErrorException(MessageConstant.UPLOAD_ERROR);
            }
        }
        Course course = Course.builder()
                .cname(cname)
                .tid(tid)
                .code(code)
                .createTime(LocalDateTime.now())
                .picture(upload)
                .build();
        courseMapper.add(course);
        return new CourseCreateVO(course.getId(), code);
    }

    @Override
    public List<Student> myStudent(MyStudentDTO myStudentDTO) {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Long> sids = courseMapper.selectByCidAndStuInfo(myStudentDTO.getCourseId(),myStudentDTO.getStudentInfo(),tid);
        if (sids == null || sids.size() == 0) return null;
        List<Student> studentList = studentMapper.selectBatchIds(sids);
        return studentList;
    }

    @Override
    public void deleteStudent(Long cid, Long sid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cid",cid);
        queryWrapper.eq("sid",sid);
        scMapper.delete(queryWrapper);
    }
}




